package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GysptWarehouse;
import com.ruoyi.gcgl.domain.GysptWarehousePicking;
import com.ruoyi.gcgl.domain.GysptWarehousePickingitem;
import com.ruoyi.gcgl.domain.GysptWarehouseitem;
import com.ruoyi.gcgl.domain.GysptWarehouseitemlog;
import com.ruoyi.gcgl.service.IGysptWarehousePickingService;
import com.ruoyi.gcgl.service.IGysptWarehousePickingitemService;
import com.ruoyi.gcgl.service.IGysptWarehouseService;
import com.ruoyi.gcgl.service.IGysptWarehouseitemService;
import com.ruoyi.gcgl.service.IGysptWarehouseitemlogService;

/**
 * 领料单Controller
 * 
 * @author ruoyi
 * @date 2024-12-01
 */
@Controller
@RequestMapping("/gcgl/picking")
public class GysptWarehousePickingController extends BaseController
{
    private String prefix = "gcgl/picking";

    @Autowired
    private IGysptWarehousePickingService gysptWarehousePickingService;
    @Autowired
    private IGysptWarehousePickingitemService gysptWarehousePickingitemService;
    @Autowired
    private IGysptWarehouseService gysptWarehouseService;
    @Autowired
    private IGysptWarehouseitemService gysptWarehouseitemService;
    @Autowired
    private IGysptWarehouseitemlogService gysptWarehouseitemlogService;

    @RequiresPermissions("gcgl:picking:view")
    @GetMapping()
    public String picking()
    {
        return prefix + "/picking";
    }

    /**
     * 查询领料单列表
     */
    @RequiresPermissions("gcgl:picking:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GysptWarehousePicking gysptWarehousePicking)
    {
        startPage();
        List<GysptWarehousePicking> list = gysptWarehousePickingService.selectGysptWarehousePickingList(gysptWarehousePicking);
        return getDataTable(list);
    }

    /**
     * 导出领料单列表
     */
    @RequiresPermissions("gcgl:picking:export")
    @Log(title = "领料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptWarehousePicking gysptWarehousePicking)
    {
        List<GysptWarehousePicking> list = gysptWarehousePickingService.selectGysptWarehousePickingList(gysptWarehousePicking);
        ExcelUtil<GysptWarehousePicking> util = new ExcelUtil<GysptWarehousePicking>(GysptWarehousePicking.class);
        return util.exportExcel(list, "领料单数据");
    }

    /**
     * 新增领料单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	mmap.put("pickingNo", ReturnUUID.getOrderNo("P"));
    	mmap.put("pickingriq", new Date());
        return prefix + "/add";
    }

    /**
     * 新增保存领料单
     */
    @RequiresPermissions("gcgl:picking:add")
    @Log(title = "领料单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptWarehousePicking gysptWarehousePicking){
    	gysptWarehousePicking.setStatus(1);
    	gysptWarehousePicking.setDeleteFlag(1);
    	gysptWarehousePicking.setCreateUser(getLoginName());
    	gysptWarehousePicking.setUpdateUser(getLoginName());
    	gysptWarehousePicking.setCreateAt(new Date());
    	gysptWarehousePicking.setUpdateAt(new Date());
    	gysptWarehousePickingService.insertGysptWarehousePicking(gysptWarehousePicking);
    	
    	for(GysptWarehousePickingitem item :gysptWarehousePicking.getItems()){
    		item.setWarehouseId(gysptWarehousePicking.getWarehouseId());
    		item.setPickingId(gysptWarehousePicking.getId());
    		item.setPickingNo(gysptWarehousePicking.getPickingNo());
    		item.setMemo("");
    		item.setStatus(1);
    		item.setDeleteFlag(1);
    		item.setCreateUser(getLoginName());
    		item.setUpdateUser(getLoginName());
    		item.setCreateAt(new Date());
    		item.setUpdateAt(new Date());
    		gysptWarehousePickingitemService.insertGysptWarehousePickingitem(item);
    	}
    	
    	addWareHouse(gysptWarehousePicking);
        return toAjax(1);
    }
    
    /**
     * 添加领料
     * @param gysptWarehousePickingitem
     * @return
     */
    private boolean addWareHouse(GysptWarehousePicking pick){
    	boolean flag = true;
    	try {
    		for(GysptWarehousePickingitem item:pick.getItems()){
    			GysptWarehouseitem gysptWarehouseitem = gysptWarehouseitemService.selectGysptWarehouseitemById(item.getItemid());
    			if(gysptWarehouseitem!=null){
    				
    				Long linglCount = 0l;
    				if(!gysptWarehouseitem.getKucshul().equals("")){
    					//领料数量+数量
    					linglCount = Long.parseLong(gysptWarehouseitem.getLinglCount()) + Long.parseLong(item.getShul());
    				}
    				gysptWarehouseitem.setLinglCount(linglCount.toString());
    				
    				gysptWarehouseitemService.updateGysptWarehouseitem(gysptWarehouseitem);//修改库存数量
    				
    				whlogEntry(gysptWarehouseitem, pick.getId(), pick.getPickingNo(), item.getShul());
    			}
        	}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    	
		return flag;
    }
    
    /**
     * 库存数量
     * @param warehouseitem		库存项
     * @param orderId			订单ID
     * @param orderNo			订单编号
     * @param shul				数量
     * @return
     */
    private boolean whlogEntry(GysptWarehouseitem warehouseitem,Long orderId,String orderNo,String shul){
    	boolean flag = true;
    	try {
			GysptWarehouseitemlog warehouseitemlog = new GysptWarehouseitemlog();
    		warehouseitemlog.setWarehouseId(warehouseitem.getWarehouseId());
    		warehouseitemlog.setOrderId(orderId);
    		warehouseitemlog.setOrderNo(orderNo);
			warehouseitemlog.setClbh(warehouseitem.getClbh());
			warehouseitemlog.setClfl(warehouseitem.getClfl());
			warehouseitemlog.setMingc(warehouseitem.getMingc());
			warehouseitemlog.setClpp(warehouseitem.getClpp());
			warehouseitemlog.setGuig(warehouseitem.getGuig());
			warehouseitemlog.setDanw(warehouseitem.getDanw());
			warehouseitemlog.setKucshul(shul);
			warehouseitemlog.setZdjine(warehouseitem.getZdjine());
			warehouseitemlog.setStatus(0);
			warehouseitemlog.setDeleteFlag(1);
			warehouseitemlog.setUpdateUser(getLoginName());
			warehouseitemlog.setUpdateAt(new Date());
			warehouseitemlog.setCreateUser(getLoginName());
			warehouseitemlog.setCreateAt(new Date());
			gysptWarehouseitemlogService.insertGysptWarehouseitemlog(warehouseitemlog);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
			return flag;
		}
    	
		return flag;
    }

    /**
     * 修改领料单
     */
    @RequiresPermissions("gcgl:picking:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptWarehousePicking gysptWarehousePicking = gysptWarehousePickingService.selectGysptWarehousePickingById(id);
        
        GysptWarehousePickingitem item = new GysptWarehousePickingitem();
        item.setDeleteFlag(1);
        item.setPickingId(id);
        List<GysptWarehousePickingitem> itemlis = gysptWarehousePickingitemService.selectGysptWarehousePickingitemList(item);
        gysptWarehousePicking.setItems(itemlis);
        
        mmap.put("gysptWarehousePicking", gysptWarehousePicking);
        
        GysptWarehouse warehouse = 
        		gysptWarehouseService.selectGysptWarehouseById(gysptWarehousePicking.getWarehouseId());
        mmap.put("warehouse", warehouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存领料单
     */
    @RequiresPermissions("gcgl:picking:edit")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptWarehousePicking gysptWarehousePicking){
    	gysptWarehousePickingService.updateGysptWarehousePicking(gysptWarehousePicking);
    	
    	addWareHouse(gysptWarehousePicking);
        return toAjax(1);
    }

    /**
     * 删除领料单
     */
    @RequiresPermissions("gcgl:picking:remove")
    @Log(title = "领料单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gysptWarehousePickingService.deleteGysptWarehousePickingByIds(ids));
    }
}
